layui.extend({
    treeSelect: '{/}http://vod.136.i2863.com/static/common/tool/treeSelect-1.0.5',   // {/}的意思即代表采用自有路径，即不跟随 base 路径
    soulTable: '{/}http://vod.136.i2863.com/static/common/layui-soul-table/1.5.14/ext/soulTable',
    tableChild: '{/}http://vod.136.i2863.com/static/common/layui-soul-table/1.5.14/ext/tableChild',
    tableMerge: '{/}http://vod.136.i2863.com/static/common/layui-soul-table/1.5.14/ext/tableMerge',
    tableFilter: '{/}http://vod.136.i2863.com/static/common/layui-soul-table/1.5.14/ext/tableFilter',
    excel: '{/}http://vod.136.i2863.com/static/common/layui-soul-table/1.5.14/ext/excel'
}).use(['form', 'layer', 'treeSelect', 'table', 'soulTable'], function () {
    var form = layui.form,
        $ = layui.jquery,
        treeSelect = layui.treeSelect,
        roleId = $("form").attr("data-id"),
        table = layui.table,
        soulTable = layui.soulTable;

    treeSelect.render({
        // 选择器
        elem: '#tree',
        // 数据
        data: '/permission/orgPerm',
        // 异步加载方式：get/post，默认get
        type: 'POST',
        // 占位符
        placeholder: '修改默认提示信息',
        // 是否开启搜索功能：true/false，默认false
        search: true,
        // 点击回调
        click: function (d) {
            table.reload('orgPermTreeList', {
                where: {roleId: roleId, zoneId: d.current.id, name: $("form input[name='name']").val()}
            });
        },
        // 加载完成后的回调函数
        success: function (d) {
            treeSelect.checkNode('tree', -1);
            init(roleId, -1)
//                选中节点，根据id筛选
//                treeSelect.checkNode('tree', 3);

//                获取zTree对象，可以调用zTree方法
//                var treeObj = treeSelect.zTree('tree');
//                console.log(treeObj);

//                刷新树结构
//                treeSelect.refresh();
        }
    });


    function init(roleId, insId) {
        table.render({
            elem: '#orgPermTree',
            url: '/permission/org/tree',
            where: {roleId: roleId, zoneId: insId, name: ''},
            page: false,
            loading: true,
            id: 'orgPermTreeList',
            cols: [[
                {type: 'checkbox', width: '60', align: 'center'},
                {field: 'name', title: '队伍名称', width: '360', align: 'center'},
                {type: 'numbers', align: 'center'}
            ]],
            overflow: {
                type: 'tips'
                , hoverTime: 300 // 悬停时间，单位ms, 悬停 hoverTime 后才会显示，默认为 0
                , color: 'black' // 字体颜色
                , bgColor: 'white' // 背景色
                , minWidth: 100 // 最小宽度
                , maxWidth: 500 // 最大宽度
            }
            , done: function (res, page, count) {
                var i = 0;
                layui.each(res.data, function (index, item) {
                    if (item.view) {
                        //可以自行添加判断的条件是否选中
                        item["LAY_CHECKED"] = true;
                        //下面三句是通过更改css来实现选中的效果
                        var current = item['LAY_TABLE_INDEX'];
                        //$('tr[data-index=' + current + '] input[type="checkbox"]+div').click();
                        $('tr[data-index=' + current + '] input[type="checkbox"]').prop('checked', true).next().addClass('layui-form-checked');
                        //这句才是真正选中，通过设置关键字LAY_CHECKED为true选中，这里只对第一行选中
                        i++;
                    }
                });

                if (i === res.data.length && i !== 0) {
                    $('thead th[data-field="0"] input[type="checkbox"]').prop('checked', true).next().addClass('layui-form-checked');
                }
                soulTable.render(this)
            }
        });

    }

    form.on('submit(search)', function (data) {
        table.reload('orgPermTreeList', {
            where: {roleId: roleId, zoneId: data.field.insId, name: data.field.name}
        });
        return false;
    });

    form.on('submit(save)', function (data) {
        var currentData = table.cache.orgPermTreeList;
        var check = [], noCheck = [];
        if (currentData.length > 0) {
            currentData.forEach(function (element, index) {
                if (element.LAY_CHECKED === true) {
                    check.push(element.orgId)
                } else {
                    noCheck.push(element.orgId)
                }
            })
        }

        layer.confirm('确定保存当前配置？', {icon: 3, title: '提示信息'}, function (index) {
            layer.close(index);
            var _index = layer.msg('数据保存中，请稍候...', {icon: 16, time: false, shade: 0.8});
            setTimeout(function () {
                parent.parent.ajax('POST',"/role/orgPerm/batchSave/" + roleId, {
                    check: check.toString(),
                    noCheck: noCheck.toString()
                }, 'json',function (res, status, xhr) {
                    layer.close(_index);
                    if (res.code == 0) {
                        layer.msg("配置成功！");
                        location.reload();
                    } else {
                        layer.msg(res.msg);
                    }
                },function () {
                    layer.close(_index);
                    layer.msg("服务器错误！");
                });
            },200)
        });
        return false;
    });
});